package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.ApCollectionMapper;
import com.usian.article.service.ApCollectionService;
import com.usian.model.article.dto.CollectionBehaviorDto;
import com.usian.model.article.pojos.ApCollection;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {

    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        ApCollection apCollection = getOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getEntryId, dto.getEntryId()).eq(ApCollection::getArticleId, dto.getArticleId()));
        if (apCollection==null){
            ApCollection collection = new ApCollection();
            collection.setEntryId(dto.getEntryId());
            collection.setArticleId(dto.getArticleId());
            collection.setType(dto.getType());
            collection.setOperation(dto.getOperation());
            collection.setCollectionTime(new Date());
            collection.setPublishedTime(new Date());
            save(collection);
        }else {
            apCollection.setOperation(dto.getOperation());
            apCollection.setCollectionTime(new Date());
            updateById(apCollection);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
